//
//  TGAlbumPhotoPreviewVC.swift
//  TGPhotoPicker
//
//  Created by targetcloud on 2017/7/22.
//  Copyright © 2017年 targetcloud. All rights reserved.
//

import UIKit
import Photos

private let cellID = "TGPhotoPreviewCell"

protocol TGPhotoCollectionDelegate:class {
    func onPreviewPageBack()
}

class TGAlbumPhotoPreviewVC: UIViewController {
    
    var fetchResult: PHFetchResult<PHAsset>?
    var currentPage: Int = 0
    weak var delegate: TGPhotoCollectionDelegate?
    
    fileprivate var isAnimation = false
    lazy var topBar:TGTopBar = {
        let topBar = TGTopBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: TGPhotoPickerConfig.shared.toolBarH))
        topBar.nav = self.nav
        topBar.selectNum = self.nav.alreadySelectedImageNum
        topBar.delegate = self
        topBar.source = self
        return topBar
    }()
    
    private lazy var topContainer:UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = TGPhotoPickerConfig.shared.barBGColor
        return view
    }()
    
    fileprivate lazy var bottomBar: TGBottomBar = {
        let subtractH: CGFloat = ((TGPhotoPickerConfig.shared.barBGColor.getAlpha()) != 1) ? 0 : 64
        let toolbar = TGBottomBar(frame: CGRect(x:0,y: 0,width: TGPhotoPickerConfig.ScreenW,height: TGPhotoPickerConfig.shared.toolBarH))
        toolbar.delegate = self
        if self.nav.assetArr.count > 0 {
            toolbar.changeNumber(number: self.nav.assetArr.count, animation: false)
        }
        return toolbar
    }()
    
    private lazy var bottomContainer:UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        let effectView:UIVisualEffectView!
        if #available(iOS 13.0, *) {
            effectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemChromeMaterial))
        } else {
            effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
        }
        view.addSubview(effectView)
        effectView.translatesAutoresizingMaskIntoConstraints = false
        effectView.translatesAutoresizingMaskIntoConstraints = false
        effectView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        effectView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        effectView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        effectView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        return view
    }()
    
    fileprivate var indicatorLabel: UILabel?
    
    fileprivate lazy var nav: TGPhotoPickerVC = self.navigationController as! TGPhotoPickerVC
    
    fileprivate lazy var cv: UICollectionView = {
        self.automaticallyAdjustsScrollViewInsets = false
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: self.view.frame.width,height: self.view.frame.height)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        if #available(iOS 13, *) {
            collectionView.backgroundColor = .systemBackground
        }
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
        collectionView.scrollsToTop = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.contentOffset = CGPoint.zero
        collectionView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(self.fetchResult!.count), height: self.view.bounds.height)
        collectionView.register(TGPhotoPreviewCell.self, forCellWithReuseIdentifier: cellID)
        self.view.addSubview(collectionView)
        
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //        self.view.backgroundColor = .white
        self.cv.reloadData()
        setupBar()
        
        if TGPhotoPickerConfig.shared.isShowIndicator && (TGPhotoPickerConfig.shared.indicatorPosition != .inBottomBar){
            indicatorLabel = (bottomBar.indicatorLabel)
            switch TGPhotoPickerConfig.shared.indicatorPosition {
            case .top:
                self.view.addSubview(indicatorLabel!)
                indicatorLabel?.origin = CGPoint(x: (TGPhotoPickerConfig.ScreenW - (bottomBar.indicatorLabel.w))/2, y: (topBar.frame.maxY) - (indicatorLabel?.h)! + 5)
            case .bottom:
                self.view.addSubview(indicatorLabel!)
                indicatorLabel?.origin = CGPoint(x: (TGPhotoPickerConfig.ScreenW - (bottomBar.indicatorLabel.w))/2, y: (bottomBar.y) - (indicatorLabel?.h)! - 5)
            default:
                self.topBar.addSubview(indicatorLabel!)
                indicatorLabel?.origin = CGPoint(x: (TGPhotoPickerConfig.ScreenW - (bottomBar.indicatorLabel.w))/2, y: ((topBar.h) - (indicatorLabel?.h)!)/2)
            }
            indicatorLabel?.isHidden = false
        }
    }
    
    override var prefersStatusBarHidden: Bool{
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
        
        if #available(iOS 9.0, *) {
            let isVCBased = Bundle.main.infoDictionary?["UIViewControllerBasedStatusBarAppearance"] as? Bool ?? false
            if !isVCBased{
                UIApplication.shared.setStatusBarHidden(true, with: .none)
            }
        }else {
            UIApplication.shared.setStatusBarHidden(true, with: .none)
        }
        
        self.cv.setContentOffset(CGPoint(x: CGFloat(self.currentPage) * self.view.bounds.width, y: 0), animated: false)
        changeCurrentToolbar()
    }
    
    fileprivate func changeCurrentToolbar(){
        if let order = nav.assetArr.index(of: self.fetchResult![self.currentPage]){
            self.topBar.setSelect(true,TGPhotoPickerConfig.shared.isShowNumber ? order : -1)
            self.bottomBar.canEdit(true)
        } else {
            self.topBar.setSelect(false)
            self.bottomBar.canEdit(false)
        }
    }
    
    private func setupBar(){
        
        self.topContainer.addSubview(topBar)
        self.view.addSubview(topContainer)
        topBar.translatesAutoresizingMaskIntoConstraints = false
        topContainer.translatesAutoresizingMaskIntoConstraints = false
        topContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        topContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        topContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        if #available(iOS 11, *) {
            let top:CGFloat = UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0.0
            topContainer.heightAnchor.constraint(equalToConstant: topBar.bounds.size.height + top).isActive = true
            topBar.topAnchor.constraint(equalTo: topContainer.safeAreaLayoutGuide.topAnchor).isActive = true
        } else {
            topContainer.heightAnchor.constraint(equalToConstant: topBar.bounds.size.height).isActive = true
            topBar.topAnchor.constraint(equalTo: topContainer.topAnchor).isActive = true
        }
        topBar.leadingAnchor.constraint(equalTo: topContainer.leadingAnchor).isActive = true
        topBar.trailingAnchor.constraint(equalTo: topContainer.trailingAnchor).isActive = true
        topBar.heightAnchor.constraint(equalToConstant: TGPhotoPickerConfig.shared.toolBarH).isActive = true
        
        
        
        self.bottomBar.delegate = self
        self.bottomBar.changeNumber(number: nav.assetArr.count, animation: false)
        self.bottomContainer.addSubview(bottomBar)
        self.view.addSubview(self.bottomContainer)
        bottomContainer.translatesAutoresizingMaskIntoConstraints = false
        bottomContainer.topAnchor.constraint(equalTo: bottomBar.topAnchor).isActive = true
        bottomContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        bottomContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        bottomContainer.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        if #available(iOS 11, *) {
            let bottom:CGFloat = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0.0
            bottomContainer.heightAnchor.constraint(equalToConstant: bottomBar.bounds.size.height + bottom).isActive = true
        } else {
            bottomContainer.heightAnchor.constraint(equalToConstant: bottomBar.bounds.size.height).isActive = true
        }
        
        bottomBar.nav = nav
        bottomBar.host = "\(type(of: self))"
    }
}

extension TGAlbumPhotoPreviewVC: TGBottomBarDelegate{
    func onDoneButtonClicked() {
        self.nav.imageSelectFinish()
    }
    
    func onEditButtonClicked(_ sender:TGAnimationButton){
        
    }
}

extension TGAlbumPhotoPreviewVC: TGTopBarDelegate{
    func onBackClicked() {
        self.navigationController?.popViewController(animated: true)
        self.delegate?.onPreviewPageBack()
    }
    
    func onSelectedClicked(select: Bool) {
        if select {
            self.nav.assetArr.append(self.fetchResult![self.currentPage] )
        } else {
            if let index = self.nav.assetArr.firstIndex(of: self.fetchResult![self.currentPage] ){
                self.nav.assetArr.remove(at: index)
            }
        }
        self.bottomBar.canEdit(select)
        self.bottomBar.changeNumber(number: self.nav.assetArr.count, animation: TGPhotoPickerConfig.shared.checkboxAnimate)
    }
}

extension TGAlbumPhotoPreviewVC: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.fetchResult?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! TGPhotoPreviewCell
        cell.delegate = self
        cell.asset = self.fetchResult![indexPath.row]
        return cell
    }
}

extension TGAlbumPhotoPreviewVC: TGPhotoPreviewCellDelegate{
    func onImageSingleTap() {
        if self.isAnimation {
            return
        }
        self.isAnimation = true
        if self.topContainer.frame.origin.y < 0 {
            UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
                self.topContainer.frame.origin = CGPoint.zero
                var originPoint = self.bottomContainer.frame.origin
                originPoint.y = originPoint.y - self.bottomContainer.frame.height
                self.bottomContainer.frame.origin = originPoint
                if TGPhotoPickerConfig.shared.indicatorPosition == .top{
                    self.indicatorLabel?.y = (self.topBar.frame.maxY) + 5
                }
                if TGPhotoPickerConfig.shared.indicatorPosition == .bottom{
                    self.indicatorLabel?.bottom = (self.bottomBar.y) - 5
                }
            }, completion: { isFinished in
                if isFinished {
                    self.isAnimation = false
                }
            })
        } else {
            UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
                self.topContainer.frame.origin = CGPoint(x:0, y: -self.topContainer.frame.height)
                var originPoint = self.bottomContainer.frame.origin
                originPoint.y = originPoint.y + self.bottomContainer.frame.height
                self.bottomContainer.frame.origin = originPoint
                if TGPhotoPickerConfig.shared.indicatorPosition == .top{
                    self.indicatorLabel?.y = (self.topBar.frame.maxY) + 5
                }
                if TGPhotoPickerConfig.shared.indicatorPosition == .bottom{
                    self.indicatorLabel?.bottom = (self.bottomBar.y) - 5
                }
            }, completion: { isFinished in
                if isFinished {
                    self.isAnimation = false
                }
            })
        }
    }
}

extension TGAlbumPhotoPreviewVC: UICollectionViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.currentPage = Int(scrollView.contentOffset.x / self.view.bounds.width)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        changeCurrentToolbar()
    }
}
